<!-- 任务目的要求 -->
<template>
  <div id="dialog-container">
    <el-button type="text" @click="dialogVisible = true">任务目的要求</el-button>
    <el-dialog v-dialogDrag title="任务目的要求" append-to-body :visible.sync="dialogVisible" width="680px" :close-on-click-modal="false">
      <el-form :model="form" :inline="true" label-width="100px" size="small">
        <el-form-item label="任务目的要求:">
          <el-input v-model="form.taskObjName"></el-input>
        </el-form-item>
        <el-form-item label="角色：">
          <el-input v-model="form.roldName" style="width: 145px;"></el-input>
          <choose-role @role="role" style="display: inline-block;"></choose-role>
        </el-form-item>
      </el-form>
      <el-button type="primary" plain size="small">添加</el-button>
      <el-table :data="tableData" size="small">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column type="index" align="center" label="序号" width="50"></el-table-column>
        <el-table-column prop="taskObjName" align="center" label="任务目的要求" min-width="100">
          <template slot-scope="scope">
            <el-input v-if="editId==scope.row.id" size="small" v-model="scope.row.taskObjName" placeholder="请输入内容"></el-input>
            <span v-if="editId!=scope.row.id">{{scope.row.taskObjName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="roldName" align="center" label="角色" min-width="100">
          <template slot-scope="scope">
            <el-input v-if="editId==scope.row.id" size="small" v-model="scope.row.roldName" placeholder="请输入内容"></el-input>
            <span v-if="editId!=scope.row.id">{{scope.row.roldName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" align="center" label="操作" min-width="100">
        <template slot-scope="scope">
            <!-- <el-button type="text">修改</el-button>
            <el-button type="text">删除</el-button> -->
            <el-button v-if="editId!=scope.row.id" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
            <el-button v-if="editId==scope.row.id" size="small" @click="editData(scope.row.id,scope.row.taskObjName,scope.row.roldName)">保存</el-button>
            <el-button v-if="editId==scope.row.id" size="small" @click="editId=''">取消</el-button>
            <el-popover placement="top" title="确认删除吗？" width="160" trigger="click" v-model="scope.row.visible">
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope.row.visible=false">取消</el-button>
                <el-button type="primary" size="mini" @click="deleteData(scope.row.id)">确定</el-button>
              </div>
              <el-button v-show="editId!=scope.row.id" size="small" type="danger" slot="reference">删除</el-button>
            </el-popover>
        </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import ChooseRole from './choose-role';
  import { getTaskObjList, saveTaskObj, deleteTaskObj } from 'service/task';
  export default {
    components: {
        'choose-role': ChooseRole
    },
    data() {
      return {
        dialogVisible: false,
        form: {
          taskObjName: '',
          roldName: ''
        },
        tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }],
        editId: '',
        isLoading: false
      }
    },
    mounted() {
      this.refreshTableData();
    },
    methods: {
      handleClose() {
        this.form.code = '';
        this.dialogVisible = false;
      },
      // 获取角色
      role(val) {
        this.form.roldName = val;
      },
      // 添加
      addCode() {
        saveTaskObj({
          taskObjName: this.form.taskObjName,
          roldName: this.form.roldName
        }).then(res => {
          if(res.data.success) {
            this.refreshTableData();
          }
        })
      },
      handleEdit(id) {
        this.editId = id;
      },
      // 删除
      deleteData(id) {
        deleteTaskObj(id)
          .then((response) => {
            this.refreshTableData();
          });
      },
      // 编辑
      editData(id, taskObjName, roldName) {
        this.editId = '';
        saveTaskObj({
          id: id,
          taskObjName: taskObjName,
          roldName: roldName
        }).then((res) => {
          if(res.data.success) {
            this.refreshTableData();
          }
        })
      },
      // 获取基础列表
      refreshTableData() {
        this.isLoading = true;
        getTaskObjList({
          taskObjName: '',
          roldName: ''
        })
          .then((res) => {
            this.tableData = res.data.result.content;
            this.isLoading = false;
          });
      },
    }
  }
</script>
